package com.sg.service.biz.app.impl;

import com.sg.common.util.RedisUtil;
import com.sg.dto.base.app.req.QueryVirtualOrgMemberDeptDetailReq;
import com.sg.dto.biz.app.req.AnalysisDashboradDeptIdComReqDto;
import com.sg.dto.biz.app.req.ImplementAnalyzeDashboradDeptIdComReqDto;
import com.sg.dto.biz.app.req.ImplementElevenReceivingFieldReqDto;
import com.sg.dto.biz.app.res.AnalysisDashboradDeptIdComRespDto;
import com.sg.dto.biz.app.res.ImplementAnalyzeDashboradDeptIdComRespDto;
import com.sg.dto.biz.app.res.ImplementElevenReceivingFieldRespDto;
import com.sg.dto.integration.framework.QueryDeptDetailComBorderReqDto;
import com.sg.dto.integration.framework.QueryDeptDetailComBorderRespDto;
import com.sg.entity.OmsVirtualOrgMemberDept;
import com.sg.service.base.app.MOmsEvaluationObjectTargetCycleService;
import com.sg.service.base.app.MOmsExecuteCycleStageService;
import com.sg.service.base.app.MOmsVirtualOrgMemberDeptService;
import com.sg.service.biz.app.DashboardTargetContentService;
import com.sg.service.biz.app.SmallCircleCommonService;
import com.sg.common.exception.Assert;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrframework.integration.OrgService;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class DashboardOrgServiceImpl
        implements com.wicket.okrapp.biz.service.DashboardOrgService {

    @Resource
    private RedisUtil redisUtil;
    //@Resource
    //private MCustomFields5Service mCustomFields5Service;
    @Resource
    private NbDashboardOrg nbDashboardOrg;
    @Resource
    private SmallCircleCommonService smallCircleCommonService;
    @Resource
    private OrgService orgService;
    @Resource
    private MOmsEvaluationObjectTargetCycleService mOmsEvaluationObjectTargetCycleService;
    @Resource
    private DashboardTargetContentService dashboardTargetContentService;
    @Resource
    private MOmsVirtualOrgMemberDeptService mOmsVirtualOrgMemberDeptService;
    @Resource
    private MOmsExecuteCycleStageService mOmsExecuteCycleStageService;

    /**
     * D3执行根据目标内容小圈子分析大屏归属部门ID(公共)[9827]
     * gen by moon at 2/12/2025, 4:15:17 PM
     */
    @Trace(operationName = "D3执行根据目标内容小圈子分析大屏归属部门ID(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AnalysisDashboradDeptIdComRespDto analysisDashboradDeptIdCom(AnalysisDashboradDeptIdComReqDto reqDto) {


        ImplementElevenReceivingFieldRespDto receptionServiceRes_1 = null;
        ImplementElevenReceivingFieldRespDto receptionServiceRes_3 = null;
//virtualUsage 3-6-03查虚拟成员部门详情  70491
        OmsVirtualOrgMemberDept omsVirtualOrgMemberDept = null;
        QueryVirtualOrgMemberDeptDetailReq queryVirtualOrgMemberDeptDetailReq = new QueryVirtualOrgMemberDeptDetailReq();
        queryVirtualOrgMemberDeptDetailReq.setDeptType("LEAD_DEPT");//sourceId:1882530_1_70491
        queryVirtualOrgMemberDeptDetailReq.setUseScene("TARGET_PLAN_SCENE");//sourceId:1882527_1_70491
        queryVirtualOrgMemberDeptDetailReq.setIsArchive("FALSE");//sourceId:1882525_1_70491
        if (reqDto != null) {
            queryVirtualOrgMemberDeptDetailReq.setEntityId(reqDto.getTargetCycleContentId());//SimpleFieldAssign//sourceId:1882523_1_70491
            queryVirtualOrgMemberDeptDetailReq.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:1882524_1_70491
        }

        /*3-6-03查虚拟成员部门详情[2518]   */
        Assert.isNull(queryVirtualOrgMemberDeptDetailReq.getEntityId(), "D3执行根据目标内容小圈子分析大屏归属部门ID(公共)-3-6-03查虚拟成员部门详情-冗余内容表主键ID不能为空", false);
        Assert.isNull(queryVirtualOrgMemberDeptDetailReq.getEvaluationTemplateId(), "D3执行根据目标内容小圈子分析大屏归属部门ID(公共)-3-6-03查虚拟成员部门详情-归属对象ID不能为空", false);
        Assert.isNull(queryVirtualOrgMemberDeptDetailReq.getDeptType(), "D3执行根据目标内容小圈子分析大屏归属部门ID(公共)-3-6-03查虚拟成员部门详情-部门类型编码不能为空", false);
        Assert.isNull(queryVirtualOrgMemberDeptDetailReq.getUseScene(), "D3执行根据目标内容小圈子分析大屏归属部门ID(公共)-3-6-03查虚拟成员部门详情-虚拟组织使用场景不能为空", false);
        Assert.isNull(queryVirtualOrgMemberDeptDetailReq.getIsArchive(), "D3执行根据目标内容小圈子分析大屏归属部门ID(公共)-3-6-03查虚拟成员部门详情-是否存档不能为空", false);
        omsVirtualOrgMemberDept = mOmsVirtualOrgMemberDeptService.queryVirtualOrgMemberDeptDetail(queryVirtualOrgMemberDeptDetailReq)/*vcase invoke 本地 method 方法调用;*/;


        if ((omsVirtualOrgMemberDept != null && omsVirtualOrgMemberDept.getDeptId() != null)) {
            //if(3-6-03查虚拟成员部门详情.虚拟组织下部门ID 值不等于空 )  70373

            QueryDeptDetailComBorderRespDto queryDeptDetailComBorderRespDto = null;
            if (omsVirtualOrgMemberDept != null) {
                QueryDeptDetailComBorderReqDto queryDeptDetailComBorderReqDto = new QueryDeptDetailComBorderReqDto();
                if (omsVirtualOrgMemberDept != null) {
                    queryDeptDetailComBorderReqDto.setDeptId(omsVirtualOrgMemberDept.getDeptId());//SimpleFieldAssign//sourceId:1882531_1_70494
                }

                /*D1-3查询部门详情(公共)(边界)[2691]   */
                Assert.isNull(queryDeptDetailComBorderReqDto.getDeptId(), "D3执行根据目标内容小圈子分析大屏归属部门ID(公共)-D1-3查询部门详情(公共)(边界)-部门ID不能为空", false);
                queryDeptDetailComBorderRespDto = orgService.queryDeptDetailComBorder(queryDeptDetailComBorderReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


            }
            if ((queryDeptDetailComBorderRespDto != null && queryDeptDetailComBorderRespDto.getLevelNumber() == 1L)) {
                //if(D1-3查询部门详情(公共)(边界).层级 等于 1)  70495

//ModelCode: receptionService
                ImplementElevenReceivingFieldRespDto receptionServiceRes = null;
                if (queryDeptDetailComBorderRespDto != null) {
                    ImplementElevenReceivingFieldReqDto receptionServiceReq = new ImplementElevenReceivingFieldReqDto();
                    if (queryDeptDetailComBorderRespDto != null) {
                        receptionServiceReq.setDashboradPrimaryDeptId(queryDeptDetailComBorderRespDto.getDeptId());//SimpleFieldAssign//sourceId:1875363_1_70497
                    }

                    /*M3-接收出参字段：大屏归属中层部门ID[9482]  用于特殊方法接收上游入参。 */
                    Assert.isNull(receptionServiceReq.getDashboradPrimaryDeptId(), "D3执行根据目标内容小圈子分析大屏归属部门ID(公共)-M3-接收出参字段：大屏归属中层部门ID-大屏归属中层部门ID不能为空", false);
                    receptionServiceRes = nbDashboardOrg.implementElevenReceivingField(receptionServiceReq);


                    receptionServiceRes_1 = receptionServiceRes;
                }
            } else if ((queryDeptDetailComBorderRespDto != null && queryDeptDetailComBorderRespDto.getLevelNumber() > 1L)) {
                //elseif(D1-3查询部门详情(公共)(边界).层级 大于 1)  70496

//ModelCode: receptionService
                ImplementElevenReceivingFieldRespDto receptionServiceRes_2 = null;
                if (queryDeptDetailComBorderRespDto != null) {
                    ImplementElevenReceivingFieldReqDto receptionServiceReq_1 = new ImplementElevenReceivingFieldReqDto();
                    if (queryDeptDetailComBorderRespDto != null) {
                        receptionServiceReq_1.setDashboradBasicDeptId(queryDeptDetailComBorderRespDto.getDeptId());//SimpleFieldAssign//sourceId:1886096_1_70570
                    }

                    /*M3-接收出参字段：大屏归属基础部门ID[9482]  用于特殊方法接收上游入参。 */
                    Assert.isNull(receptionServiceReq_1.getDashboradBasicDeptId(), "D3执行根据目标内容小圈子分析大屏归属部门ID(公共)-M3-接收出参字段：大屏归属基础部门ID-大屏归属基层部门ID不能为空", false);
                    receptionServiceRes_2 = nbDashboardOrg.implementElevenReceivingField(receptionServiceReq_1);


                    receptionServiceRes_3 = receptionServiceRes_2;
                }
            } else {
                //else  70379

//异常结束 70380
                throw new BizException("10300461", "对不起，找不到部门信息~", false);
            }
        } else if ((omsVirtualOrgMemberDept == null)) {
            //elseif(3-6-03查虚拟成员部门详情.出参 值等于空 )  70492

            if ((reqDto != null && reqDto.getRespBodyType() != null && reqDto.getRespBodyType().equals("PERSONAL"))) {
                //if(D3执行根据目标内容小圈子分析大屏归属部门ID(公共).责任主体类型 等于 个人)  73870

//processBranchName:正常结束 ,processBranchId:73873

            } else {
                //else  73871

//异常结束 73872
                throw new BizException("10300460", "对不起，找不到目标内容牵头部门信息~", false);
            }
        }
        AnalysisDashboradDeptIdComRespDto retData = new AnalysisDashboradDeptIdComRespDto();
        if (receptionServiceRes_1 != null) {
            retData.setDashboradPrimaryDeptId(receptionServiceRes_1.getDashboradPrimaryDeptId());//SimpleFieldAssign//sourceId:1875365_1
        }
        if (receptionServiceRes_3 != null) {
            retData.setDashboradBasicDeptId(receptionServiceRes_3.getDashboradBasicDeptId());//SimpleFieldAssign//sourceId:1886102_1
        }


        return retData;
    }

    /**
     * D3执行分析大屏归属部门ID(公共)[9843]
     * gen by moon at 6/25/2024, 9:50:44 AM
     */
    @Trace(operationName = "D3执行分析大屏归属部门ID(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ImplementAnalyzeDashboradDeptIdComRespDto implementAnalyzeDashboradDeptIdCom(ImplementAnalyzeDashboradDeptIdComReqDto reqDto) {


        ImplementElevenReceivingFieldRespDto receptionServiceRes_3 = null;
//virtualUsage M3-约定字段：中层部门、基层部门  70550
        //ModelCode: receptionService
        ImplementElevenReceivingFieldRespDto receptionServiceRes = null;
        ImplementElevenReceivingFieldReqDto receptionServiceReq = new ImplementElevenReceivingFieldReqDto();
        receptionServiceReq.setCustomField1("MID_DEPT");//CUSTOM_CONVENTION//sourceId:1885358_1_70550
        receptionServiceReq.setCustomField2("ROOT_DEPT");//CUSTOM_CONVENTION//sourceId:1885359_1_70550

        /*M3-约定字段：中层部门、基层部门[9482]  用于特殊方法接收上游入参。 */
        Assert.isNull(receptionServiceReq.getCustomField1(), "D3执行分析大屏归属部门ID(公共)-M3-约定字段：中层部门、基层部门-自定义字段1不能为空", false);
        Assert.isNull(receptionServiceReq.getCustomField2(), "D3执行分析大屏归属部门ID(公共)-M3-约定字段：中层部门、基层部门-自定义字段2不能为空", false);
        receptionServiceRes = nbDashboardOrg.implementElevenReceivingField(receptionServiceReq);


//virtualUsage D1-3查询部门详情(公共)(边界)  70544
        QueryDeptDetailComBorderRespDto queryDeptDetailComBorderRespDto = null;
        QueryDeptDetailComBorderReqDto queryDeptDetailComBorderReqDto = new QueryDeptDetailComBorderReqDto();
        if (reqDto != null) {
            queryDeptDetailComBorderReqDto.setDeptId(reqDto.getDeptId());//SimpleFieldAssign//sourceId:1884838_1_70544
        }

        /*D1-3查询部门详情(公共)(边界)[2691]   */
        Assert.isNull(queryDeptDetailComBorderReqDto.getDeptId(), "D3执行分析大屏归属部门ID(公共)-D1-3查询部门详情(公共)(边界)-部门ID不能为空", false);
        queryDeptDetailComBorderRespDto = orgService.queryDeptDetailComBorder(queryDeptDetailComBorderReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        if ((queryDeptDetailComBorderRespDto != null && queryDeptDetailComBorderRespDto.getLevelNumber() == 1L)) {
            //if(D1-3查询部门详情(公共)(边界).层级 等于 1)  70545

//ModelCode: receptionService
            ImplementElevenReceivingFieldRespDto receptionServiceRes_2 = null;
            ImplementElevenReceivingFieldReqDto receptionServiceReq_1 = new ImplementElevenReceivingFieldReqDto();
            if (receptionServiceRes != null) {
                receptionServiceReq_1.setBelongToPersonalIdentityRoleCode(receptionServiceRes.getCustomField1());//SimpleFieldAssign//sourceId:1885103_1_70547
            }
            if (reqDto != null) {
                receptionServiceReq_1.setDashboradPrimaryDeptId(reqDto.getDeptId());//SimpleFieldAssign//sourceId:1885091_1_70547
            }

            /*M3-接收出参字段：大屏归属中层部门ID、大屏归属基层部门ID[9482]  用于特殊方法接收上游入参。 */
            Assert.isNull(receptionServiceReq_1.getBelongToPersonalIdentityRoleCode(), "D3执行分析大屏归属部门ID(公共)-M3-接收出参字段：大屏归属中层部门ID、大屏归属基层部门ID-数据归属对象角色编码不能为空", false);
            receptionServiceRes_2 = nbDashboardOrg.implementElevenReceivingField(receptionServiceReq_1);


            receptionServiceRes_3 = receptionServiceRes_2;
        } else if ((queryDeptDetailComBorderRespDto != null && queryDeptDetailComBorderRespDto.getLevelNumber() > 1L)) {
            //elseif(D1-3查询部门详情(公共)(边界).层级 大于 1)  70546

//ModelCode: receptionService
            ImplementElevenReceivingFieldRespDto receptionServiceRes_4 = null;
            ImplementElevenReceivingFieldReqDto receptionServiceReq_2 = new ImplementElevenReceivingFieldReqDto();
            if (receptionServiceRes != null) {
                receptionServiceReq_2.setBelongToPersonalIdentityRoleCode(receptionServiceRes.getCustomField2());//SimpleFieldAssign//sourceId:1885103_1_70548
            }
            if (reqDto != null) {
                receptionServiceReq_2.setDashboradBasicDeptId(reqDto.getDeptId());//SimpleFieldAssign//sourceId:1885092_1_70548
            }

            /*M3-接收出参字段：大屏归属中层部门ID、大屏归属基层部门ID[9482]  用于特殊方法接收上游入参。 */
            Assert.isNull(receptionServiceReq_2.getBelongToPersonalIdentityRoleCode(), "D3执行分析大屏归属部门ID(公共)-M3-接收出参字段：大屏归属中层部门ID、大屏归属基层部门ID-数据归属对象角色编码不能为空", false);
            receptionServiceRes_4 = nbDashboardOrg.implementElevenReceivingField(receptionServiceReq_2);


            receptionServiceRes_3 = receptionServiceRes_4;
        }
        ImplementAnalyzeDashboradDeptIdComRespDto retData = new ImplementAnalyzeDashboradDeptIdComRespDto();
        if (receptionServiceRes_3 != null) {
            retData.setBelongToPersonalIdentityRoleCode(receptionServiceRes_3.getBelongToPersonalIdentityRoleCode());//SimpleFieldAssign//sourceId:1885360_1
            retData.setDashboradPrimaryDeptId(receptionServiceRes_3.getDashboradPrimaryDeptId());//SimpleFieldAssign//sourceId:1885093_1
            retData.setDashboradBasicDeptId(receptionServiceRes_3.getDashboradBasicDeptId());//SimpleFieldAssign//sourceId:1885094_1
        }


        return retData;
    }
    //
}
